package net.dgg.rz.production.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.dgg.framework.tac.utils.DggValidateUtil;
import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.exception.DggBaseException;
import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.service.CmsService;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.excption.CommonExcption;
import net.dgg.rz.common.service.CommonMangerExtService;
import net.dgg.rz.common.service.CommonService;
import net.dgg.rz.common.systemRecord.entity.SystemRecord;
import net.dgg.rz.common.systemRecord.service.SystemRecordService;
import net.dgg.rz.common.utils.*;
import net.dgg.rz.production.entity.Assistant;
import net.dgg.rz.production.entity.ProductOrder;
import net.dgg.rz.production.entity.dto.ProductOrderDto;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.rz.production.service.ProductOrderService;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className ProcessingOrderController
 * @Author Quanlin Wan
 * @Description //TODO 处理中
 * @Date 2018/12/17 18:27
 **/
@Controller
@RequestMapping(value = "/rzsc/processing_order")
public class ProcessingOrderController extends DggBaseController {
    private final static String PATH_PREFIX = "/rzsc/processing_order";
    @Autowired
    private ProductOrderService productOrderService;
    @Autowired
    DesUtil desUtil;
    @Autowired
    private CommonMangerExtService commonMangerExtService;
    @Autowired
    DataPermissionUtils dataPermissionUtils;
    @Autowired
    private CmsService cmsService;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private SystemRecordService systemRecordService;
    @Autowired
    private CommonService commonService;

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 处理中页面
     * @Date 15:29 2018/12/18
     * @Param
     **/

    @RequestMapping("/index.html")
    public String index(Model model) {
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "rzsc/processingorder/processing_order";
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 待接收页面
     * @Date 9:24 2018/12/21
     * @Param
     **/


    @RequestMapping("/wait_accept.html")
    public String waitAccept(Model model, String code) {
        model.addAttribute("code", code);
        return "rzsc/processingorder/waitAccept";
    }

    /**
     * @return
     * @Author cuipeng
     * @Description //TODO 接单
     * @Date 9:24 2019/01/29
     * @Param
     **/
    @RequestMapping("/receive_order.html")
    @SuppressWarnings("all")
    public String receiveOrder(Model model, String id) {
        try {
            if (!StringUtils.isEmpty(id)) {
                //-------------获取当前生产订单数据---------------------------------
                ProductOrder productOrder = productOrderService.selectByPrimaryKey(Long.parseLong(id));
                if (productOrder != null) {
                    model.addAttribute("productOrder", productOrder);
                } else {
                    throw new ProductOrderExcption("未查询到主订单，请刷新列表");
                }
            } else {
                throw new ProductOrderExcption("未获取到主订单id");
            }
            return "rzsc/processingorder/receive_order";
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/receive_order.html","id"+ id);
            throw new ProductOrderExcption(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 办理中页面
     * @Date 9:27 2018/12/21
     * @Param
     **/


    @RequestMapping("/inProcess.html")
    public String inProcess(Model model) {
        return "rzsc/processingorder/in_process";
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 已暂缓页面
     * @Date 9:39 2018/12/21
     * @Param
     **/
    @RequestMapping("/defedOrder.html")
    public String defedOrder(Model model) {
        return "rzsc/processingorder/defed_order";
    }


    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 处理中页面分页数据
     * @Date 15:29 2018/12/18
     * @Param
     **/

    @RequestMapping("/list.do")
    @ResponseBody
    public DggRestResponse list(@RequestParam Map params) {
        try {
            List<ProductOrderDto> productOrderDtos = productOrderService.processingscOrderWithPage(params);
            return new DataTableResponse().data(params, productOrderDtos);
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/list.do","params"+ params.toString());
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 打开退单页面
     * @Date 10:10 2018/12/25
     * @Param
     **/

    @RequestMapping("/abandonPage.html")
    public String abandonPage(Model model) {
        List list = commonMangerExtService.queryTreeBookListByCode(ScProductConstant.TD_YY_CODE, 1, null);
        model.addAttribute("list", list);
        return "rzsc/processingorder/abandon_dialog";
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 申请退单
     * @Date 19:00 2018/12/25
     * @Param
     **/

    @RequestMapping("/abandon_reason_order.do")
    @ResponseBody
    public Object abandonReasonOrder(@RequestParam String params) {
        try {
            this.productOrderService.abandonReasonOrder(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/abandon_reason_order.do","params"+ params);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 打开废单页面
     * @Date 10:10 2018/12/25
     * @Param
     **/

    @RequestMapping("/nullifyPage.html")
    public String nullifyPage(Model model) {
        List list = commonMangerExtService.queryTreeBookListByCode(ScProductConstant.FD_YY_CODE, 1, null);
        model.addAttribute("list", list);
        return "rzsc/processingorder/nullify_dialog";
    }

    /**
     * @return
     * @Author cuipeng
     * @Description //TODO 打开订单退回页面
     * @Date 2019/1/10
     * @Param
     **/
    @RequestMapping("/back_order.html")
    public String backOrder(Model model) {
        return "rzsc/processingorder/backOrder_dialog";
    }

    /**
     * @return
     * @Author cuipeng
     * @Description //TODO 转单退回
     * @Date 2019/1/10
     * @Param
     **/
    @RequestMapping("/backorder_save")
    @ResponseBody
    public DggRestResponse backorderSave(String id, String remark) {
        try {
            this.productOrderService.backorderSave(id, remark, null);
            return this.getSuccessResponse("操作成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/backorder_save.do","id"+ id+",remark"+remark);
            return this.getFailResponse(e.getMessage());
        }
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 申请废单
     * @Date 19:00 2018/12/25
     * @Param
     **/

    @RequestMapping("/nullify_reason_order.do")
    @ResponseBody
    public Object nullifyReasonOrder(@RequestParam String params) {
        try {
            this.productOrderService.nullifyReasonOrder(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/nullify_reason_order.do","params"+ params);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 打开暂缓页面
     * @Date 10:10 2018/12/25
     * @Param
     **/

    @RequestMapping("/suspendPage.html")
    public String suspendPage(Model model) {

        List list = commonMangerExtService.queryTreeBookListByCode(ScProductConstant.ZH_YY_CODE, 1, null);
        model.addAttribute("list", list);
        return "rzsc/processingorder/suspend_dialog";

    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 暂缓
     * @Date 19:00 2018/12/25
     * @Param
     **/

    @RequestMapping("/suspend_reason_order.do")
    @ResponseBody
    public DggRestResponse suspendReasonOrder(@RequestParam String params) {
        try {
            productOrderService.suspendReasonOrder(params);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/suspend_reason_order.do","params"+ params);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * 单个接收订单
     *
     * @param
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/single_receive_order", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse singleReceiveOrder(String params) {
        try {
            JSONObject jsonObject = JSON.parseObject(params);
            productOrderService.singleReceiveOrder(jsonObject);
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/single_receive_order.do","params"+ params);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
        return this.getSuccessResponse("接收成功,当前订单已进入办理中");
    }

    /**
     * 接收订单(批量)
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/multi_receive_order", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse multiReceiveOrder(String orderIds) {
        try {
            ValidateUtils.strNotEmpty(orderIds, CommonExcption.class, "订单id不能为空");
            productOrderService.receiveOrder(orderIds,null);
            return this.getSuccessResponse("接收成功,当前订单已进入办理中");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/multi_receive_order.do","orderIds"+ orderIds);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * 转单弹窗页面
     *
     * @param model
     * @param status 状态
     * @return
     */
    @RequestMapping(value = "/transfer_order_page")
    public String transferOrderPage(Model model, String status) {
        return "rzsc/processingorder/transfer_order_page";
    }

    /**
     * 订单转单
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/transfer_order", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse transferOrder(String orderIds, Long transferUserId, String transferOrderRemark) {
        try {
            ValidateUtils.strNotEmpty(orderIds, CommonExcption.class, "订单id不能为空");
            ValidateUtils.strNotEmpty(transferUserId, CommonExcption.class, "转单人不能为空");
            Map<String, String> result = productOrderService.transferOrder(orderIds, transferUserId, transferOrderRemark, null);
            if ("1".equals(result.get("flag"))) {
                return this.getSuccessResponse("转单成功");
            } else {
                return this.getFailResponse(result.get("msg"));
            }
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/transfer_order.do","orderIds:"+ orderIds+",transferUserId"+transferUserId+",transferOrderRemark"+transferOrderRemark);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    //------------------------------------------------zyou---------------------------------------------------

    /**
     * 新增渠道
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/add_channel.do", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse addChannel(@RequestParam Map params) {
        try {
            productOrderService.addChannel(params);
            return this.getSuccessResponse("新增成功");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/add_channel.do","params:"+ params.toString());
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * <p>@Description 打开添加协单，获取数据</p>
     * <p>@author mqy</p>
     * <p>@Time 2018/8/16 0016 16:26 创建时间</p>
     * <p>@return</p>
     */


    /**
     * 返回添加协单人页面
     *
     * @param id    主订单id
     * @param model
     * @return
     */
    @RequestMapping("/addAssistPage.html")
    public String addAssistPage(Long id, Model model) {
        try {
            if (!StringUtils.isEmpty(id)) {
                //-------------获取当前生产订单数据---------------------------------
                ProductOrder productOrder = productOrderService.selectByPrimaryKey(id);
                if (productOrder != null) {
                    StringBuilder ids = new StringBuilder();
                    StringBuilder names = new StringBuilder();

                    model.addAttribute("productOrder", productOrder);

                    //协单人员
                    List<Assistant> scAssistantList = productOrderService.queyScAssistantList(id);
                    if (scAssistantList != null && !scAssistantList.isEmpty()) {
                        for (Assistant scAssistant : scAssistantList) {
                            ids.append(scAssistant.getAssistUserId().toString().concat(","));//协助人员id
                            names.append(scAssistant.getAssistUserName().concat(","));//协助人员名称
                        }
                        model.addAttribute("ids", ids.deleteCharAt(ids.lastIndexOf(",")));
                        model.addAttribute("names", names.deleteCharAt(names.lastIndexOf(",")));
                    }
                } else {
                    throw new ProductOrderExcption("未查询到主订单，请刷新列表");
                }
            } else {
                throw new ProductOrderExcption("未获取到主订单id");
            }
            return "rzsc/processingorder/add_assist_page";
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/addAssistPage.html","id:"+ id);
            throw new ProductOrderExcption(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * 保存协办人员更新
     *
     * @param productOrderId 主订单id
     * @param assistIds      协办人员id集合
     * @return
     */
    @RequestMapping(value = "/updateAssist.do")
    @ResponseBody
    public Object updateAssist(Long productOrderId, String assistIds) {
        try {
            //验证当前登录用户是否可以操作该主订单
            productOrderService.validationUpdateNode(productOrderId, "updateAssist",null);

            productOrderService.updateAssist(productOrderId, assistIds);
            return this.getSuccessResponse("操作成功!");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/updateAssist.do","productOrderId:"+ productOrderId+":assistIds"+assistIds);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * 返回更新渠道页面
     *
     * @param id    主订单id
     * @param model
     * @return
     */
    @RequestMapping("/update_channel.html")
    public String updateChannel(Long id, Model model) {
        try {
            if (!StringUtils.isEmpty(id)) {
                //-------------获取当前生产订单数据---------------------------------
                ProductOrder productOrder = productOrderService.selectByPrimaryKey(id);
                if (productOrder != null) {
                    model.addAttribute("productOrder", productOrder);

                } else {
                    throw new ProductOrderExcption("未查询到主订单，请刷新列表");
                }
            } else {
                throw new ProductOrderExcption("未获取到主订单id");
            }

            return "rzsc/processingorder/updateChannel";
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/update_channel.html","id:"+ id);
            throw new ProductOrderExcption(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * 返回添加渠道页面
     *
     * @param id    主订单id
     * @param model
     * @return
     */
    @RequestMapping("/add_channel_html.html")
    public String addChannelHtml(Long id, Model model) {
        try {
            if (!StringUtils.isEmpty(id)) {
                //-------------获取当前生产订单数据---------------------------------
                ProductOrder productOrder = productOrderService.selectByPrimaryKey(id);
                if (productOrder != null) {
                    model.addAttribute("productOrder", productOrder);

                } else {
                    throw new ProductOrderExcption("未查询到主订单，请刷新列表");
                }
            } else {
                throw new ProductOrderExcption("未获取到主订单id");
            }

            return "rzsc/processingorder/add_channel";
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            throw new ProductOrderExcption(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/add_channel_html.do","id:"+ id);
            throw new ProductOrderExcption(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * 根据产品id查询渠道
     *
     * @param id 产品id
     * @return
     */
    @RequestMapping("/channel_by_proid.do")
    @ResponseBody
    public DggRestResponse channelByProid(Long id) {
        try {
            if (!StringUtils.isEmpty(id)) {
                List<Map> maps = commonService.channelByProid(id + "");
                if (maps != null && maps.size() > 0) {
                    return this.getSuccessResponse(maps);
                } else {
                    return this.getFailResponse("根据产品id未查询到渠道");
                }
            } else {
                throw new ProductOrderExcption("未获取到产品id");
            }

        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/channel_by_proid.do","id:"+ id);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * 根据产品id查询商品(弃用)
     *
     * @param id 产品id
     * @return
     */
    @RequestMapping("/goods_by_proid.do")
    @ResponseBody
    public DggRestResponse goodsByProid(Long id) {
        try {
            if (!StringUtils.isEmpty(id)) {
                Map map = new HashMap();
                map.put("proId", id);
                Map goods = cmsService.productInfo(map);
                List<Map<String, Object>> list = (List<Map<String, Object>>) goods.get("goodsList");
                if (list != null && list.size() > 0) {
                    return this.getSuccessResponse(list);
                } else {
                    return this.getFailResponse("根据产品id未查询到商品");
                }
            } else {
                throw new ProductOrderExcption("未获取到产品id");
            }

        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/goods_by_proid.do","id:"+ id);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * 更新主订单办理渠道
     *
     * @param id 产品id
     * @return
     */
    @RequestMapping("/update_current_channel.do")
    @ResponseBody
    public DggRestResponse updateCurrentChannel(Long id, Long channelId, String subBranch) {
        try {
            if (!StringUtils.isEmpty(id) && !StringUtils.isEmpty(channelId)) {
                ProductOrder productOrder = productOrderService.selectByPrimaryKey(id);//查询主订单
                if (productOrder != null) {
                    productOrderService.validationUpdateNode(productOrder.getId(), "updateCurrentChannel",null);//验证是否可以操作
                    UserEntity userEntity = sessionManager.getCurrentUser();
                    //根据渠道id，查询渠道
                    HashMap<Object, Object> map = new HashMap<>();
                    map.put("channelId", channelId);
                    ValidateUtils.isTrue(channelId.longValue() != productOrder.getChannelId().longValue(), ProductOrderExcption.class, "不能选择相同渠道！");
                    Map map1 = cmsService.proChannelInfo(map);//查询渠道详情
                    if (map1 != null) {

                        //克隆
                        //获取生产子订单编号
                        String number = cmsService.getBillNumber("sc");
                        DggValidateUtil.notNull(number, CommonExcption.class, "单据号获取失败,请联系管理员！");
                        ProductOrder newProductOrder = BeanUtils.copy(productOrder, ProductOrder.class);
                        newProductOrder.setScProductOrderNo(number);
                        newProductOrder.setId(DggKeyWorker.nextId());
                        newProductOrder.setFlag(1);//作废

                        productOrder.setChannelId(Long.parseLong(map1.get("id").toString()));//渠道id
                        productOrder.setChannelName(map1.get("name").toString());//渠道名称
                        productOrder.setFlag(0);
                        if (subBranch != null) {
                            productOrder.setSubBranch(subBranch);//设置支行
                            newProductOrder.setSubBranch(subBranch);
                        }
                        productOrderService.updateChannel(productOrder, newProductOrder);//更新渠道
                        //添加操作记录
                        SystemRecord systemRecord = new SystemRecord();
                        systemRecord.setWorkId(productOrder.getId());
                        systemRecord.setWorkNo(productOrder.getScProductOrderNo());
                        systemRecord.setWorkTableName("rzsc_product_order");
                        systemRecord.setSource("2");//来源生产
                        systemRecord.setOperateType("41");//操作类型 : 更换办理渠道
                        systemRecord.setRemarks("生产单更换渠道");
                        systemRecordService.save(systemRecord, userEntity, null);//保存上被操作人
                        return this.getSuccessResponse("操作成功");
                    } else {
                        return this.getFailResponse("渠道不存在，请刷新列表");
                    }
                } else {
                    return this.getFailResponse("主订单不存在，请刷新列表");
                }
            } else {
                throw new ProductOrderExcption("未获取到产品id或渠道id");
            }

        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/update_current_channel.do","id:"+ id+",channelId:"+channelId+"subBranch:"+subBranch);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * 导出(处理中)
     */
    @RequestMapping(value = "/excel_export_processing_order.do")
    @ResponseBody
    public void exportDetailExcelProcessingOrder(@RequestParam Map params) {
        int[] widths = new int[]{700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8, 700 * 8};
        int[] styleArr = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        List<ProductOrderDto> list = productOrderService.processingscOrderWithPage(params);
        String target = params.get("target").toString();
        String sheetName = new String();
        String[] title = null;
        String[] keyArray = null;
        switch (target) {
            case "0":
                sheetName = "待接收生产订单数据";
                title = new String[]{"生产订单编号", "订单编号", "客户名称", "客户电话", "当前完成节点", "服务合同编号", "签单时间", "签单人", "签单人部门", "流程人员", "流程人员部门", "协单人员", "业务类型", "产品", "渠道", "来源方式", "支行"};
                keyArray = new String[]{"scProductOrderNo", "orderNo", "customerName", "customerPhone", "latestEndNodeName", "contractNo", "orderTime", "signUserName", "signUserOrgName", "flowUserName", "flowUserOrgName", "assistUserName", "businessProductName", "productName", "channelName", "originCode", "subBranch"};
                ExcelTool.execlExport(list, sheetName, title, keyArray, widths, styleArr);
                break;
            case "1":
                sheetName = "处理中生产订单数据";
                title = new String[]{"生产订单编号", "订单编号", "客户名称", "客户电话", "当前完成节点", "最新完成节点时间", "服务合同编号", "签单时间", "接单时间", "签单人", "签单人部门", "流程人员", "流程人员部门", "协单人员", "业务类型", "产品", "渠道", "来源方式", "支行", "需求金额", "资金用途"};
                keyArray = new String[]{"scProductOrderNo", "orderNo", "customerName", "customerPhone", "latestEndNodeName", "latestEndNodeTime", "contractNo", "orderTime", "receiveOrderDate", "signUserName", "signUserOrgName", "flowUserName", "flowUserOrgName", "assistUserName", "businessProductName", "productName", "channelName", "originCode", "subBranch", "loanAmount", "purposeStr"};
                ExcelTool.execlExport(list, sheetName, title, keyArray, widths, styleArr);
                break;
            default:
                sheetName = "已暂缓生产订单数据";
                title = new String[]{"生产订单编号", "订单编号", "客户名称", "客户电话", "暂缓时间", "暂缓原因", "服务合同编号", "签单时间", "接单时间", "签单人", "签单人部门", "流程人员", "流程人员部门", "业务类型", "产品", "渠道", "支行", "所属事业部"};
                keyArray = new String[]{"scProductOrderNo", "orderNo", "customerName", "customerPhone", "suspendTime", "suspendReason", "contractNo", "orderTime", "receiveOrderDate", "signUserName", "signUserOrgName", "flowUserName", "flowUserOrgName", "businessProductName", "productName", "channelName", "subBranch", "productOrgName"};
                ExcelTool.execlExport(list, sheetName, title, keyArray, widths, styleArr);
                break;
        }

    }


    /**
     * @param scOrderId 生产订单ID
     * @param model
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 返回添加产品页面
     * @Date 9:46 2019/3/22
     **/
    @RequestMapping("/add_product_page.html")
    public String addProductPage(Long scOrderId, Model model) {

        ProductOrder productOrder = productOrderService.selectByPrimaryKey(scOrderId);
        model.addAttribute("productOrder", productOrder);
        return "rzsc/processingorder/add_product";
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 返回更换产品页面
     * @Date 14:16 2019/3/29
     * @Param
     **/
    @RequestMapping("/change_product_page.html")
    public String changeProductPage(Long scOrderId, Model model) {

        ProductOrder productOrder = productOrderService.selectByPrimaryKey(scOrderId);
        model.addAttribute("productOrder", productOrder);
        return "rzsc/processingorder/change_product";
    }


    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO商品信息页面(弃用)
     * @Date 9:18 2019/3/25
     * @Param
     **/

    @RequestMapping("/product_info.html")
    public String addProductPage(@RequestParam String params, Model model) {
        try {
            JSONObject jsonObject = JSON.parseObject(params);
            ValidateUtils.strNotEmpty(jsonObject.getLong("productId"), ProductOrderExcption.class, "产品id不能为空");
            ValidateUtils.strNotEmpty(jsonObject.getString("choosedProduct"), ProductOrderExcption.class, "没有选择对应业务类型或产品");
            Map map = new HashMap();
            map.put("proId", jsonObject.getLong("productId"));
            Map goods = cmsService.productInfo(map);
            model.addAttribute("goodsList", goods.get("goodsList"));
            String choosedProduct = jsonObject.getString("choosedProduct");
            Long goodId = jsonObject.getLong("goodId");
            model.addAttribute("goodId", goodId);
            model.addAttribute("productId", jsonObject.getLong("productId"));
            model.addAttribute("choosedProduct", choosedProduct);
        } catch (DggBaseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            ExceptionUtils.error(e, PATH_PREFIX+"/product_info.do","params:"+ params);
            e.printStackTrace();
        }
        return "rzsc/processingorder/product_info";
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 新增产品
     * @Date 9:46 2019/3/22
     * @Param
     **/
    @RequestMapping("/add_product.do")
    @ResponseBody
    public DggRestResponse addProduct(String params) {
        try {
            JSONObject jsonObject = JSON.parseObject(params);
            productOrderService.addProduct(jsonObject);
            return this.getSuccessResponse("操作成功");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/add_product.do","params:"+ params);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 更改产品
     * @Date 9:41 2019/3/28
     * @Param
     **/
    @RequestMapping("/change_product.do")
    @ResponseBody
    public DggRestResponse changeProduct(String params) {
        try {
            JSONObject jsonObject = JSON.parseObject(params);
            productOrderService.changeProduct(jsonObject, ScProductConstant.RZSC_ORIGINCODE_PRO_CHANGE);
            return this.getSuccessResponse("操作成功");
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/change_product.do","params:"+ params);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @return 0为老数据，1为新数据
     * @Author Quanlin Wan
     * @Description //TODO 验证订单是老流程还是新流程（4月22日兼容老数据，绕过更换产品）
     * @Date 8:57 2019/4/23
     * @Param
     **/

    @RequestMapping("/verify_old")
    @ResponseBody
    public DggRestResponse verifyOld(String id) {

        try {
            Integer verify = productOrderService.verifyOld(id);
            return this.getSuccessResponse(verify);
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/verify_old.do","id"+ id);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }
}

